<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$return['params']=array();
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby=array();
	$fields=array('id','title','createtime','plugin','status');
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('core_diy',$params,array($pindex,$psize),$total,$fields,'id',$orderby);
	if($list){
		foreach($list as &$row){
			$row['createtime']=date('20y/m/d H:i:s',$row['createtime']);

			if($row['plugin']=='core'){
				$row['plugin']='系统';
			}else{
				$row['plugin']=pdo_getcolumn('core_plugins',array('name'=>$row['plugin']),'title');
			}
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['statuss']=array(
		'0'=>'禁用',
		'1'=>'启用',
	);
	if($_SESSION['isfounder']){
		$return['isfounder']=1;
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			'0'=>array('op'=>'list','title'=>'自定义页面列表'),
			'1'=>array('op'=>'edit','title'=>'添加自定义页面'),
			'2'=>array('op'=>'edit','title'=>'同步云端组件库'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'页面名称','type'=>'string'),
			array('name'=>'status','title'=>'页面状态','type'=>'switch'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
			array('op'=>'made','title'=>'设计','type'=>'href','html'=>'basic/diy'),
			array('op'=>'generate','title'=>'生成','type'=>'href'),
		)),
		'search_one'=>array('name'=>'title','add'=>false),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_diy',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('id','plugin','action','type','title'));
		if($item['type']==2){
			$parameter=array(
				'plugin'=>'core',
				'action'=>'diy',
				'id'=>$id
			);
			$back=cfc('core')->get_back_news($parameter);
			if($back){
				$item['description']=$back['description'];
				$item['keyword']=$back['keyword'];
				$item['picurl']=$back['picurl'];
			}
		}
	}else{
		$item=array();
	}
	$return['item']=$item;
	$plugins=pdo_getall('core_plugins',array('is_new'=>1),array('name','title'),'name');
	$pages=cache_read('diy_pages','core');
	if(!$pages){
		$p=array_keys($plugins);
		$post=array('plugins'=>base64_encode(json_encode($p)));
		$pages=cloud_request('diy.mobile.getpages',$post);
		if(is_error($pages)){
			exi($pages['message'],'error');
		}
		cache_write('diy_pages',$pages,'core','页面diy——插件页面数据');
	}
	foreach($pages as &$page){
		if($page['fields']){
			unset($page['fields']);
		}
	}
	$return['plugins']=$plugins;
	$return['pages']=$pages;
	$return['types']=array(
		'1'=>'系统插件页面',
		'2'=>'新页面'
	);
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'页面',
		'tabs'=>array(
			array('op'=>'list','title'=>'自定义页面列表'),
			array('op'=>'edit','title'=>'添加自定义页面'),
			array('op'=>'get','title'=>'同步云端组件库'),
		),
		'edit'=>array(
			array('type'=>'radio','title'=>'页面类型','name'=>'type', 'options'=>'types','help'=>'系统插件页面会覆盖现有系统插件相应页面，可以获取并展现相应页面数据。新页面则不会覆盖任何页面，直接通过单独的入口访问。' ),
			array('type'=>'switch','title'=>'是否启用','name'=>'status'),
			array('type'=>'string','title'=>'页面名称','name'=>'title', 'help'=>'','show'=>array('type',2)),
			array('type'=>'string','title'=>'页面关键词','name'=>'keyword','help'=>'设置页面关键词后，可在公众号中输入该关键词触发进入该页面','show'=>array('type',2)),
			array('type'=>'string','title'=>'封面简介','name'=>'description','help'=>'','show'=>array('type',2)),
			array('type'=>'image','title'=>'封面图片','name'=>'picurl' , 'help'=>'','show'=>array('type',2)),
			array('type'=>'select','title'=>'选择插件','name'=>'plugin','options'=>'plugins','show'=>array('type',1),'help'=>'本功能暂时只支持diy已有系统或者插件页面'),
			array('type'=>'select','title'=>'选择插件','name'=>'action','options'=>'pages[pagedata.item.plugin]','show'=>array('type',1),'help'=>'本功能暂时只支持diy已有系统或者插件页面'),

		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$type=intval($postdata['type']);
	if(!$type){
		exi('请先选择页面类型！','error');
	}
	if($type==1){
		$data=array(
			'type'=>$type,
			'plugin'=>trim($postdata['plugin']),
			'action'=>trim($postdata['action']),
		);
		$pages=cache_read('diy_pages','core');
		$data['title']=$pages[$data['plugin']][$data['action']]['title'];
	}else{
		$data=array(
			'type'=>$type,
			'plugin'=>'core',
			'action'=>'diy',
			'title'=>trim($postdata['title'])
		);
		
	}
	$data['status']=intval($postdata['status']);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('core_diy',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('core_diy',$data);
		$id=pdo_insertid();
	}
	if($postdata['keyword']){
		//添加回复规则
		$parameter=array('plugin'=>'core');
		$parameter['action']='diy';
		$parameter['id']=$id;
		$parameter['keyword']=trim($postdata['keyword']);
		$parameter['title']=$data['title'];
		$parameter['description']=$postdata['description'];
		$parameter['picurl']=$postdata['picurl'];
		$parameter['status']=$postdata['status'];
		cfc('core')->save_back_news($parameter);
	}
	exi('操作成功！');
}
if($_RQ['op']=='made'){
	//exi('cs','error');
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('core_diy',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['items']=$item['items']?json_decode($item['items'],true):array();
		//print_r($item['items']);exit;
		$item['setting']=$item['setting']?json_decode($item['setting'],true):array();
	}else{
		$item=array('items'=>array(),'setting'=>array('fstyle'=>array(),'gstyle'=>array()));
	}
	$return['item']=$item;
	$navs=pdo_getall('core_mobile_blocks',array(),array(),'','usenum desc',array('10'));
	$return['navs']=$navs;
	$pages=cache_read('diy_pages','core');
	$plugins=pdo_getall('core_plugins',array('is_new'=>1),array('name','title'),'name');
	if(!$pages){
		$p=array_keys($plugins);
		$post=array('plugins'=>base64_encode(json_encode($p)));
		$pages=cloud_request('diy.mobile.getpages',$post);
		if(is_error($pages)){
			exi($pages['message'],'error');
		}
		cache_write('diy_pages',$pages,'core','页面diy——插件页面数据');
	}
	$fields=$pages[$item['plugin']][$item['page']]['fields'];
	$return['data']=$fields;
	exi($return);
}
if($_RQ['op']=='postmade'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'setting'=>json_encode($postdata['setting']),
		'items'=>json_encode($postdata['items']),
		'status'=>intval($postdata['status']),
		'action'=>trim($postdata['action'])
	);
	//检测恶意代码
	foreach($data as $row){
		if(strexists($row,'<?')){
			exi('提交内容存在恶意代码，无法提交！','error');
		}
	}
	$id=intval($postdata['id']);
	if($id){
		pdo_update('core_diy',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('保存页面成功！');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('core_diy',$data);
		exi('新增页面成功！');
	}
}
if($_RQ['op']=='generate'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
		
	}
	$item=pdo_get('core_diy',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),array('items','setting','title','id','html'));
	$post=array('items'=>base64_encode($item['items']),'setting'=>base64_encode($item['setting']));
	$html=cloud_request('diy.mobile.made',$post);
	if(is_error($html)){
			exi($html['message'],'error');
		}
	//将获得生成的html代码存入文件
	$html=cfc('files')->source_save($html,'page','html',$item['title'],$item['html']);
	if(!$item['html']){
		pdo_update('core_diy',array('html'=>$html),array('id'=>$item['id']));
	}
	exi('生成成功！');
}
if($_RQ['op']=='get'){
	//同步云端模版组件
	$navs=cloud_request('diy.mobile.get');
	if(is_error($navs)){
			exi($navs['message'],'error');
		}
	if($navs){
		foreach($navs as &$row){
			$check=pdo_get('core_mobile_blocks',array('cloud_id'=>$row['id']),array('id','edithtml','showhtml'));
			$row['showhtml']=cfc('files')->source_save($row['showhtml'],'page','html',$row['title'],$check['showhtml']);
			$row['edithtml']=cfc('files')->source_save($row['edithtml'],'page','html',$row['title'],$check['edithtml']);
			if(!$check){
				$row['cloud_id']=$row['id'];
				unset($row['id']);
				pdo_insert('core_mobile_blocks',$row);
			}
		}
	}
	exi('同步成功！');
}
if($_RQ['op']=='getnav'){
	
	$params=array();
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
	}
	$navs=pdo_getall('core_mobile_blocks',$params,array(),'','usenum desc',array('10'));
	exi($navs);
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('参数错误！','error');
	}
	$status=intval($_RQ['status']);
	pdo_update('core_diy',array('status'=>$status),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_diy',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			if(intval($id)){
				pdo_delete('core_diy',array('id'=>intval($id),'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}

